package com.vtuzx.kingdee.xqc_core.dao;

import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 对接系统接口履历Dao
 */
@Component
public class IntegrationHistoryDao extends VtuzxDaoImpl {
    /** 集合名称 */
    public static final String NAME = "t_integration_history";

    public IntegrationHistoryDao() {
        super(NAME);
    }

    /**
     * 统计店铺数据
     */
    public Iterator<Document> statStore(ObjectId _storeId, Date from, Date to) {
        Criteria cond = Criteria.where("_storeId").is(_storeId);
        cond.and("from").is("store");
        cond.and(IVtuzxConst.Key_createTime).gte(from).lte(to);

        List<Object> createTimeList = new ArrayList<>();
        createTimeList.add("$createTime");
        createTimeList.add(28800000);
        Document project1 = new Document(IVtuzxConst.Key__id, 0)
                .append("status", 1)
                .append("bjCreateTime", new Document("$add", createTimeList));

        Document project2 = new Document("status", 1)
                .append("createYmd", new Document("$dateToString", new Document("format", "%Y-%m-%d").append("date", "$bjCreateTime")));

        Document groupId = new Document("status", "$status").append("createTime", "$createYmd");

        List<Document> pipes = new ArrayList<>();
        pipes.add(new Document("$match", cond.getCriteriaObject()));
        pipes.add(new Document("$project", project1));
        pipes.add(new Document("$project", project2));
        pipes.add(new Document("$group", new Document(IVtuzxConst.Key__id, groupId).append("count", new Document("$sum", 1))));
        return aggregate(pipes);
    }

    /**
     * 统计对接系统数据
     */
    public Iterator<Document> statIntegrate(ObjectId _storeId, Date from, Date to) {
        Criteria cond = Criteria.where("_storeId").is(_storeId);
        cond.and("from").is("conn");
        cond.and(IVtuzxConst.Key_createTime).gte(from).lte(to);

        List<Object> createTimeList = new ArrayList<>();
        createTimeList.add("$createTime");
        createTimeList.add(28800000);
        Document project1 = new Document(IVtuzxConst.Key__id, 0)
                .append("event", 1)
                .append("bjCreateTime", new Document("$add", createTimeList));

        Document project2 = new Document("event", 1)
                .append("createYmd", new Document("$dateToString", new Document("format", "%Y-%m-%d").append("date", "$bjCreateTime")));

        Document groupId = new Document("event", "$event").append("createTime", "$createYmd");

        List<Document> pipes = new ArrayList<>();
        pipes.add(new Document("$match", cond.getCriteriaObject()));
        pipes.add(new Document("$project", project1));
        pipes.add(new Document("$project", project2));
        pipes.add(new Document("$group", new Document(IVtuzxConst.Key__id, groupId).append("count", new Document("$sum", 1))));
        return aggregate(pipes);
    }
}